package com.otitan.la.forest.system.controller;

import com.otitan.la.forest.system.dto.AreaDTO;
import com.otitan.la.forest.system.entity.AreaEntity;
import com.otitan.la.forest.system.service.AreaService;
import com.otitan.la.forest.system.vo.AreaVO;
import com.otitan.webapp.framework.basepro.model.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 行政区划表
 *
 * @author sunke
 * @email xxx
 * @date 2020-10-09 15:04:19
 */
@Slf4j
@Api(tags = "行政区划表API")
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaService areaService;


    @ApiOperation(value = "新增")
    @PostMapping
    public DataResult add(@Valid @RequestBody AreaEntity area, BindingResult bindingResult) {
        return areaService.add(area, bindingResult);
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/batchDelete")
    public DataResult batchDelete(@RequestBody @ApiParam(value = "ID集合") List<String> ids) {
        return areaService.batchDelete(ids);
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("/delete/{id}")
    public DataResult delete(@PathVariable("id") String id) {
        List<String> ids = new ArrayList<String>() {{
            add(id);
        }};
        return areaService.batchDelete(ids);
    }

    @ApiOperation(value = "更新")
    @PutMapping
    public DataResult editor(@Valid @RequestBody AreaEntity area, BindingResult bindingResult) {
        return areaService.editor(area, bindingResult);
    }

    @ApiOperation(value = "列表查询（分页）")
    @PostMapping("/findPage/{page}/{size}")
    public DataResult findPageData(@PathVariable("page") long page,
                                     @PathVariable("size") long size,
                                     @RequestBody AreaDTO area) {
        return areaService.findPageData(page, size, area);
    }

    @ApiOperation(value = "根据ID查询")
    @GetMapping("/findOne/{id}")
    public DataResult findOne(@PathVariable("id") String id) {
        return areaService.findOne(id);
    }

    @ApiOperation(value = "获取行政区划树")
    @GetMapping("findAreaTree")
    public DataResult<List<AreaVO>> geAreaTree() {
        List<AreaVO> listTree=areaService.getAreaTree();
        return DataResult.success(listTree);
    }

    @ApiOperation(value = "获取行政区划树（新增组织机构用）")
    @GetMapping("findEffectiveAreaTree")
    public DataResult<List<AreaVO>> findAreaTreeByTwoLevels() {
        return areaService.findAreaTreeByTwoLevels();
    }

    @ApiOperation(value = "行政区划失效接口")
    @GetMapping("UnEffectArea/{id}")
    public DataResult<List<AreaVO>> UnEffectArea(@PathVariable("id") String id) {
        return areaService.makeAreaUneffect(id);
    }

    @ApiOperation(value = "通过父级编码查询子地区")
    @GetMapping("findCodeByParent")
    public DataResult findCodeByParent(String parentCode){
        return areaService.findCodeByParent(parentCode);
    }

}
